package com.ruoqing.gateway.config;

import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebExceptionHandler;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
	* @author YaoXian
	* @title: JsonSentinelGatewayBlockExceptionHandler
	* @projectName ruoxi-blog
	* @description: 若兮微服务 - 网关服务 - 限流友好提示
	* @date 2020-11-23 18:28
	*/
public class JsonSentinelGatewayBlockExceptionHandler implements WebExceptionHandler {
		public JsonSentinelGatewayBlockExceptionHandler(List<ViewResolver> viewResolvers, ServerCodecConfigurer serverCodecConfigurer) {
		}
		
		@Override
		public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
				ServerHttpResponse serverHttpResponse = exchange.getResponse();
				serverHttpResponse.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
				byte[] data = "{\"code\":403,\"msg\":\"API接口被限流\"}".getBytes(StandardCharsets.UTF_8);
				DataBuffer buffer = serverHttpResponse.bufferFactory().wrap(data);
				return serverHttpResponse.writeWith(Mono.just(buffer));
		}
}